package com.weimob.mcloud.wechatwork.customer.externalDependencyProxy.usermanagement;

import com.alibaba.dubbo.config.annotation.Reference;
import com.weimob.marketing.syswechatworkusermanagement.wechatworkusermanagement.base.BasicInfo;
import com.weimob.marketing.syswechatworkusermanagement.wechatworkusermanagement.service.user.PermissionExport;
import com.weimob.marketing.syswechatworkusermanagement.wechatworkusermanagement.service.user.dto.req.QueryVidReq;
import com.weimob.marketing.syswechatworkusermanagement.wechatworkusermanagement.service.user.dto.res.vid.RoleVidResp;
import com.weimob.mcloud.wechatwork.core.entity.privatesphere.PrivateSphere;
import com.weimob.mcloud.wechatwork.customer.enums.ErrorCodeEnum;
import com.weimob.mcloud.wechatwork.statistics.exception.BizException;
import com.weimob.soa.common.utils.SoaUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * 用户数据隔离相关接口
 *
 * @Author LeoLee
 * @Date 2022/8/22 19:44
 * @Version 1.0
 */
@Slf4j
@Component
public class UserPermissionProxy {

    @Reference
    private PermissionExport permissionExport;

    /**
     * 批量获取员工对应的vid集合（获取员工的权限范围）
     * @Author LeoLee
     * @Date 21:18 2022/8/22
     */
    public RoleVidResp getRoleVid(PrivateSphere privateSphere, List<String> employeeIdList,Long wid) {
        if (privateSphere == null) {
            log.warn("根据员工查询对应权限vidList失败:私域为空");
            throw new BizException(ErrorCodeEnum.PRIVATE_SPHERE_IS_NULL.getCode(), ErrorCodeEnum.PRIVATE_SPHERE_IS_NULL.getErrorDesc());
        }
        QueryVidReq queryVidReq = new QueryVidReq();
        BasicInfo basicInfo = new BasicInfo();
        basicInfo.setBosId(privateSphere.getBosId());
        basicInfo.setProductId(privateSphere.getProductId());
        basicInfo.setProductInstanceId(privateSphere.getProductInstanceId());
        queryVidReq.setBasicInfo(basicInfo);
        if(!CollectionUtils.isEmpty(employeeIdList)){
            queryVidReq.setEmployeeIds(employeeIdList);
        }
        if(Objects.nonNull(wid)){
            queryVidReq.setWid(wid);
            queryVidReq.setAllVid(Boolean.FALSE);
        }
        RoleVidResp roleVidResp = SoaUtil.unpack(permissionExport.getRoleVid(queryVidReq));
        return roleVidResp;
    }
}
